package net.esj.basic.view;

import java.util.List;
import java.util.ResourceBundle;

import net.esj.basic.core.engine.support.message.MessageProvider;
import net.esj.basic.core.engine.support.message.MessageProvidersFactory;
import net.esj.basic.utils.Validators;

import org.apache.commons.lang.StringUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * 该类实现从spring注入的MessageProvider
 * 默认从类名出发寻找同名的properties文件中的内容
 * @author Administrator
 *
 */
public abstract class SpringDependActionSupport extends ActionSupport {

	private MessageProvider messageProvider = MessageProvidersFactory.getInstance();
	
	@Override
	public String getText(String aTextName, List<?> args) {
		String reval = null;
		if(messageProvider!=null){
			reval =  messageProvider.getMessage(getClass().getName(), aTextName, args.toArray(), getLocale());
		}
		if((Validators.isEmpty(reval) || StringUtils.equals(aTextName, reval)) && getDefaultSources()!=null){
			for(String source : getDefaultSources()){
				reval =  messageProvider.getMessage(source, aTextName, args.toArray(), getLocale());
			}
		}
		if(reval==null){
			return super.getText(aTextName,args);
		}			
		return reval;
	}

	@Override
	public String getText(String key, String defaultValue, List<?> args,
			ValueStack stack) {
		String reval = null;
		if(messageProvider!=null){
			reval =  messageProvider.getMessage(getClass().getName(), key, args.toArray(), defaultValue,getLocale());
		}
		if(reval == null){
			return super.getText(key,defaultValue,args,stack);
		}
		return reval;
	}

	@Override
	public String getText(String aTextName, String defaultValue, List<?> args) {
		String reval = null;
		if(messageProvider!=null){
			reval =  messageProvider.getMessage(getClass().getName(), aTextName, args.toArray(),defaultValue, getLocale());
		}
		
		if(reval == null){
			return super.getText(aTextName,defaultValue,args);
		}
		return reval;
	}

	@Override
	public String getText(String aTextName, String defaultValue, String obj) {
		String reval = null;
		if(messageProvider!=null){
			reval =  messageProvider.getMessage(getClass().getName(), aTextName, new Object[]{obj},defaultValue, getLocale());
		}
		
		if(reval == null){
			return super.getText(aTextName,defaultValue,obj);
		}
		return reval;
	}

	@Override
	public String getText(String key, String defaultValue, String[] args,
			ValueStack stack) {
		String reval = null;
		if(messageProvider!=null){
			reval =  messageProvider.getMessage(getClass().getName(), key, args,defaultValue, getLocale());
		}
		
		if(reval == null){
			return super.getText(key,defaultValue,args,stack);
		}
		return reval;
	}

	@Override
	public String getText(String key, String defaultValue, String[] args) {
		String reval = null;
		if(messageProvider!=null){
			reval =  messageProvider.getMessage(getClass().getName(), key, args,defaultValue, getLocale());
		}

		if(reval == null){
			return super.getText(key,defaultValue,args);
		}
		return reval;
	}

	@Override
	public String getText(String aTextName, String defaultValue) {
		String reval = null;
		if(messageProvider!=null){
			reval =  messageProvider.getMessage(getClass().getName(), aTextName, null,defaultValue, getLocale());
		}
		
		if((Validators.isEmpty(reval) || StringUtils.equals(aTextName, reval))&& getDefaultSources()!=null ){
			for(String source : getDefaultSources()){
				reval =  messageProvider.getMessage(source, aTextName, null,aTextName, getLocale());
			}
		}
		return reval;
		
	}

	@Override
	public String getText(String key, String[] args) {
		String reval = null;
		if(messageProvider!=null){
			reval =  messageProvider.getMessage(getClass().getName(), key,args, key,getLocale());
		}
		
		if((Validators.isEmpty(reval) || StringUtils.equals(key, reval)) && getDefaultSources()!=null){
			for(String source : getDefaultSources()){
				reval =  messageProvider.getMessage(source, key, args, getLocale());
			}
		}
		
		if(reval == null){
			return super.getText(key,args);
		}
		return reval;
		
	}

	@Override
	public String getText(String aTextName) {
		String reval = null;
		if(messageProvider!=null){
			reval =  messageProvider.getMessage(getClass().getName(), aTextName,null, getLocale());
		}
		
		if((Validators.isEmpty(reval) || StringUtils.equals(aTextName, reval))&& getDefaultSources()!=null ){
			for(String source : getDefaultSources()){
				reval =  messageProvider.getMessage(source, aTextName, null,aTextName, getLocale());
			}
		}
		
		if(reval == null){
			return super.getText(aTextName);
		}
		return reval;
		
	}
	
	/**
	 * 直接从某个properties文件获取
	 * @param propertiesName
	 * @param aTextName
	 * @return
	 */
	public String getProperty(String propertiesName,String aTextName) {
		String reval = null;
		if(messageProvider!=null){
			reval =  messageProvider.getMessage(propertiesName, aTextName,null,aTextName, getLocale());
		}
		
		if(reval == null){
			return super.getText(aTextName);
		}
		return reval;
	}
	
	public String getProperty(String propertiesName,String aTextName,String defaultValue) {
		String reval = null;
		if(messageProvider!=null){
			reval = messageProvider.getMessage(propertiesName, aTextName, null,defaultValue, getLocale());
		}
		if(reval == null){
			return super.getText(aTextName);
		}
		return reval;
	}
	
	public String getProperty(String propertiesName,String aTextName,String[] args) {
		String reval = null;
		if(messageProvider!=null){
			reval =  messageProvider.getMessage(propertiesName, aTextName ,args, getLocale());
		}
		if(reval == null){
			return super.getText(aTextName);
		}
		return reval;
	}

	@Override
	public ResourceBundle getTexts() {
		return super.getTexts();
	}

	@Override
	public ResourceBundle getTexts(String aBundleName) {
		return super.getTexts(aBundleName);
	}
	
	protected String[] getDefaultSources(){
		return new String[]{"message"};
	}
	
/*	@Autowired
	public void setMessageProvider(MessageProvider messageProvider) {
		this.messageProvider = messageProvider;
	}*/
}
